package com.xinsoft.controller.mes;

import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesFeedingPlanDTO;
import com.xinsoft.entity.dto.MesFeedingPlanPageParam;
import com.xinsoft.entity.po.MesFeedingPlan;
import com.xinsoft.service.MesFeedingPlanService;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName MesFeedingPlanController
 * @Description 投料计划控制层
 * @Author hu
 * @Date 2023-7-17 15:43:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/mes/feedingPlan")
public class MesFeedingPlanController {

    @Autowired
    private MesFeedingPlanService mesFeedingPlanService;

    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Response<?> add(@RequestBody MesFeedingPlanDTO mesFeedingPlan) {
        // 参数校验
        ValidatorUtils.validateEntity(mesFeedingPlan);
        sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.MES_FEEDING_PLAN_DOCUMENT);
        return Response.succeed(mesFeedingPlanService.addMesFeedingPlan(mesFeedingPlan));
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  MesFeedingPlanDTO mesFeedingPlan) {
        // 参数校验
        ValidatorUtils.validateEntity(mesFeedingPlan);
        return Response.succeed(mesFeedingPlanService.editMesFeedingPlan(mesFeedingPlan));
    }

    /**
     * 获取详细
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(mesFeedingPlanService.getMesFeedingPlan(id));
    }


    /**
     * 审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/audit")
    public Response<?> audit(@RequestBody List<Integer> ids) {
        mesFeedingPlanService.audit(ids);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/deAudit")
    public Response<?> deAudit(@RequestBody List<Integer> ids) {
        mesFeedingPlanService.deAudit(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody MesFeedingPlanPageParam mesFeedingPlanPageParam) {
        return Response.succeed(mesFeedingPlanService.findPageListByParam(mesFeedingPlanPageParam));
    }

    /**
     * 根据id获取明细行
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/getItemList")
    public Response<?> getItemList(@RequestBody List<Integer> ids) {
        return Response.succeed(mesFeedingPlanService.getItemList(ids));
    }
}
